#include "storage.h"
#include <QFile>

Storage::Storage(QObject *parent) :
    QObject(parent)
{
}

QList<Info> Storage::load(QString const& fileName)
{
    QList<Info> list;
    QFile file(fileName);
    if (file.open(QIODevice::ReadOnly))
    {
        QByteArray qba;
        while(!file.atEnd())
        {
            Info info;
            qba = file.readLine();
            qba.chop(1);
            info.name = qba;
            qba = file.readLine();
            qba.chop(1);
            info.surname = qba;
            qba = file.readLine();
            qba.chop(1);
            info.email = qba;
            qba = file.readLine();
            qba.chop(1);
            info.phone = qba;
            qba = file.readLine();
            qba.chop(1);
            info.notes = qba;
            list.push_back(info);
        }
        file.close();
    }
    return list;
}

void Storage::save(QString const& fileName, QList<Info> data)
{
    QFile file(fileName);
    if (file.open(QIODevice::WriteOnly))
    {
        for(int i = 0; i < data.size(); ++i)
        {
            Info info = data[i];
            file.write((info.name + "\n").toStdString().c_str());
            file.write((info.surname + "\n").toStdString().c_str());
            file.write((info.email + "\n").toStdString().c_str());
            file.write((info.phone + "\n").toStdString().c_str());
            file.write((info.notes + "\n").toStdString().c_str());
        }
        file.close();
    }
}
